package com.sfh.cpc.user;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sfh.cpc.com.ImageLoaderEngine;
import com.sfh.cpc.com.entity.ECity;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.entity.RspBase;
import com.sfh.cpc.com.req.ReqUserRegister;
import com.sfh.cpc.com.ui.SelectTakeImageActivity;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.user.model.RegisterModel;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.sfh.cpc.R;
/**
 * 功能描述: 注册
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/16
 */
public class UserRegisterActivity extends SelectTakeImageActivity<RegisterModel> implements View.OnClickListener {

    public static void startUI(Context context) {

        Intent intent = new Intent(context, UserRegisterActivity.class);
        context.startActivity(intent);
    }


    ImageView ivHeader;
    EditText etNumber;
    EditText etPwd;
    TextView tvAddress;
    RadioButton rbFaMale, rbMale;
    ReqUserRegister req = new ReqUserRegister();
    WidgetBaseToolbar toolbar;
    View pb;
    ImageView ivOK;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_register_activity);
        toolbar  = findViewById(R.id.toolbar);
        ivHeader = findViewById(R.id.ivHeader);
        etNumber = findViewById(R.id.etNumber);
        etPwd = findViewById(R.id.etPwd);
        tvAddress = findViewById(R.id.tvAddress);
        rbFaMale = findViewById(R.id.rbFaMale);
        rbMale = findViewById(R.id.rbMale);
        pb = findViewById(R.id.pb);
        ivOK = findViewById(R.id.ivOK);
        toolbar.setTitle("注册").setTextColor(Color.BLACK);

        ivHeader.setOnClickListener(this);

        tvAddress.setOnClickListener(this);
        findViewById(R.id.btOK).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                req.setSex(rbFaMale.isChecked() ? "0" : "1");
                req.setPwd(etPwd.getText().toString());
                req.setName(etNumber.getText().toString());
                req.setAddress(tvAddress.getText().toString());
                getViewModel().save(req);
            }
        });
        findViewById(R.id.tv_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                LoginActivity.startUI(UserRegisterActivity.this);
                finish();
            }
        });

    }


    @Override
    public void onSelectImageOK(List<String> select) {
        pb.setVisibility(View.VISIBLE);
        new ImageLoaderEngine(this,new File(select.get(0))).resizeDimen(R.dimen.px_100, R.dimen.px_100).placeholder(R.drawable.bg_add_photo).error(R.drawable.bg_add_photo).centerCrop().into(ivHeader);
        getViewModel().fileSave(select.get(0));
    }

    @Override
    public void onClick(View v) {
        if (v == ivHeader) {
            this.hasPermissions();
            return;
        }
        if (tvAddress == v) {
            this.getViewModel().queryCiry();
            return;
        }

    }

    @LiveDataMatch
    public void selectCity(final List<ECity> data) {

        final List<List<ECity>> options2Items = new ArrayList<>();

        for (ECity city : data) {
            // 市
            options2Items.add(city.getChildList());
        }


        //条件选择器
        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String tx = options2Items.get(options1).get(option2).getAreaNm();
                if (TextUtils.equals("市辖区",tx)){
                    tx = data.get(options1).getAreaNm();
                }
                tvAddress.setText(tx);
            }
        }).build();
        pvOptions.setPicker(data, options2Items);
        pvOptions.show();
    }

    @LiveDataMatch
    public void onSaveFile(RspBase<String> data) {
        if (data == null){
            pb.setVisibility(View.GONE);
            ivOK.setVisibility(View.VISIBLE);
            ivOK.setImageResource(R.drawable.live_btn_close);
            return;
        }
        pb.setVisibility(View.GONE);
        ivOK.setVisibility(View.VISIBLE);
        if (data.success()) {
            ivOK.setImageResource(R.drawable.activity_charge_pay_item_selected);
            req.setUrl(data.getData());
        } else {
            ivOK.setImageResource(R.drawable.live_btn_close);
            this.showToast(data.getMsg());
        }
    }

    @LiveDataMatch
    public void onSave(RspBase<EUser>   data) {

        if (data.success()){
            this.finish();
        }
    }

}
